package org.seckill.web;

import java.util.Date;
import java.util.List;

import org.seckill.dto.Exposer;
import org.seckill.dto.SeckillExecution;
import org.seckill.dto.SeckillResult;
import org.seckill.entity.Seckill;
import org.seckill.enums.SeckillEnum;
import org.seckill.exception.RepeatKillException;
import org.seckill.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/seckill")
public class SeckillController {
	
	@Autowired
	private SeckillService seckillService;
	
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String list(Model model){
		List<Seckill> list  = seckillService.getSeckillList();
		model.addAttribute("list", list);
		
		return "list";
	}
	
	@RequestMapping(value="/{seckillId}/detail",method=RequestMethod.GET)
	public String detail(@PathVariable(value="seckillId")Long seckillId,Model model){
		System.out.println(seckillId);
		if (seckillId==null) {
			return "redirect:/seckill/list";
		}
		Seckill seckill = seckillService.getById(seckillId);
		if (seckill==null) {
			return "forward:/seckill/list";
		}
		model.addAttribute("seckill", seckill);
		return "detail";
	}
	
	@ResponseBody
	@RequestMapping(value="/exposer",method=RequestMethod.POST)
	public SeckillResult<Exposer> exposer(Long seckillId){
		SeckillResult<Exposer> result;
		Exposer exposer;
		try {
			exposer = seckillService.exportSeckillUrl(seckillId);
			result = new SeckillResult<Exposer>(true, exposer);
		} catch (Exception e) {
			result = new SeckillResult<Exposer>(false, e.getMessage());
		}
		System.out.println(result);
		System.out.println(result.getData());
		return result;
		
	}
	
	@ResponseBody
	@RequestMapping(value="/{seckillId}/{md5}/execution",method=RequestMethod.POST)
	public SeckillResult<SeckillExecution> execute(@PathVariable(value="seckillId")Long seckillId,
		   @CookieValue(value="killPhone",required=false) Long userPhone,@PathVariable(value="md5")String md5) {
		SeckillResult<SeckillExecution> result;
		
		try {
			SeckillExecution seckillExecution = seckillService.executeSeckill(seckillId,userPhone, md5);
			result = new SeckillResult<SeckillExecution>(true, seckillExecution);
		} catch (RepeatKillException e) {
			result = new SeckillResult<SeckillExecution>(false, e.getMessage());
		}
		
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="/time",method=RequestMethod.GET)
	public SeckillResult<Long> time(){
		SeckillResult<Long> result;
		Date date =  new Date();
		result  = new SeckillResult<Long>(true, date.getTime());
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="/test",method=RequestMethod.GET)
	public SeckillExecution test(){
		SeckillExecution seckill = new SeckillExecution(12345, SeckillEnum.SUCCESS);
		return seckill;
	}
}
